package com.dianrui.agent.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dianrui.agent.R;
import com.dianrui.agent.bean.VioOrder;


import org.json.JSONArray;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ViolationOrderListAdapter extends BaseAdapter {
    Context context;
    BatteryClickListener batteryClickListener;
    List<VioOrder> repairList;


    public ViolationOrderListAdapter(Context context, List<VioOrder> repairList) {
        this.context = context;
        this.repairList=repairList;
    }
    public void refresh(List<VioOrder> list) {
        this.repairList = list;
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (repairList == null) {
            return 0;
        }
        return repairList.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public interface BatteryClickListener {
        void onBatteryClick(String order_code, String number, String title, String remark, String money, String address, String create_time, JSONArray photo);

    }

    public void setOnBatteryClickListener(BatteryClickListener messageClickListener) {
        this.batteryClickListener = messageClickListener;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_violation_order_list, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        if (repairList.size()>0) {
            final VioOrder vioOrder = repairList.get(position);
            holder.carNumbers.setText(vioOrder.order_code);
            holder.number.setText(vioOrder.vehicle_number);
            holder.title.setText(vioOrder.title);
            holder.create_time.setText(vioOrder.create_time);
            if (vioOrder.state.equals("0")) {
                holder.status.setText("未处理");
                holder.status.setTextColor(Color.RED);
            }
            if (vioOrder.state.equals("1")) {
                holder.status.setText("已处理");
                holder.status.setTextColor(Color.GREEN);
            }
            if (vioOrder.state.equals("2")) {
                holder.status.setText("未通过");
                holder.status.setTextColor(Color.RED);
            }
            if (vioOrder.state.equals("3")) {
                holder.status.setText("已通过");
                holder.status.setTextColor(Color.GREEN);
            }
            if (vioOrder.state.equals("4")) {
                holder.status.setText("已撤销");
                holder.status.setTextColor(Color.GRAY);
            }
            holder.click.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    batteryClickListener.onBatteryClick(vioOrder.order_code,vioOrder.vehicle_number,vioOrder.title,vioOrder.remark,vioOrder.money,vioOrder.address,vioOrder.create_time,vioOrder.photo);

                }
            });

        }

        return convertView;
    }


     class ViewHolder {
        @BindView(R.id.numbers)
        TextView carNumbers;
        @BindView(R.id.status)
        TextView status;
         @BindView(R.id.title)
         TextView title;
         @BindView(R.id.number)
         TextView number;
         @BindView(R.id.create_time)
         TextView create_time;
         @BindView(R.id.repair_item_layout)
        LinearLayout click;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
